﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BookStore.Common;
using System.Data.SqlClient;
using BookStore.Models;
using System.Data;
using System.Windows.Forms;

namespace BookStore.Controllers
{
    public class CatalogueController : Controller
    {
        private static CatalogueController _controller = null;
        public static CatalogueController getController()
        {
            if (_controller == null)
                return new CatalogueController();
            else
                return _controller;
        }
        public CatalogueController()
        {
            _controller = this;
        }

        public String getCatalogue(String catalogue, int check)
        {
            DataTable dt = new DataTable("GetCatalogue");
            con = BSCommon.getConnection();
            try
            {
                con.Open();
                // Get Catalogue from CatalogueID, check = 0
                if (check == 0)
                {
                    cmd = new SqlCommand("BS_GetCatalogueName", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@CatalogueID", SqlDbType.NVarChar).Value = catalogue;
                }
                // Get CatalogueID from CatalogueName, check = 1
                else
                {
                    cmd = new SqlCommand("BS_GetCatalogueID", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@CatalogueName", SqlDbType.NVarChar).Value = catalogue;
                }
                var result = cmd.ExecuteReader();
                dt.Load(result);
                return dt.Rows[0][0].ToString();
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                con.Dispose();
                con.Close();
            }
        }

        public DataTable getAllCatalogue()
        {
            DataTable all = new DataTable("Catalogue");
            con = BSCommon.getConnection();
            con.Open();
            cmd = new SqlCommand("select * from Catalogue", con);
            cmd.CommandType = CommandType.Text;
            SqlDataReader sdr = cmd.ExecuteReader();
            all.Load(sdr);
            con.Dispose();
            con.Close();
            return all;
        }
    }
}
